const express = require('express')
const router = express.Router()
const handler = require('../utils/handler')
const cheerio = require('cheerio');

// test
router.get('/', (req, res) => {
    res.send('服务已正常启动...')
})

// 微博
router.get('/weibo', (req, res) => {
    const options = {
        TITLE: '微博',
        CACHE_KEY: 'weibo',
        URL: 'https://weibo.com/ajax/statuses/hot_band'
    }
    handler(req, res, options, (res) => {
        return res.data.band_list.map(v => {
            return {
                title: v.word,
                desc: v.note,
                hotCount: v.raw_hot,
                url: `https://s.weibo.com/weibo?q=${encodeURIComponent(v.word_scheme ? v.word_scheme : v.word)}`
            }
        })
    })
})

// 知乎
router.get('/zhihu', (req, res) => {
    const options = {
        TITLE: '知乎',
        CACHE_KEY: 'zhihu',
        URL: 'https://www.zhihu.com/api/v4/creators/rank/hot?domain=0&period=hour'
    }
    handler(req, res, options, (res) => {
        return res.data.map(v => {
            return {
                title: v.question.title,
                desc: v.question.title,
                hotCount: v.reaction.pv,
                url: v.question.url
            }
        })
    })
})

// 百度
router.get('/baidu', (req, res) => {
    const options = {
        TITLE: '百度',
        CACHE_KEY: 'baidu',
        URL: 'https://top.baidu.com/board?tab=realtime'
    }
    handler(req, res, options, (res) => {
        const result = res.match(/<\!--s-data:(.*?)-->/s);
        return JSON.parse(result[1]).cards[0].content.map(v => {
            return {
                title: v.word,
                desc: v.desc,
                pic: v.img,
                hotCount: v.hotScore,
                url: `https://www.baidu.com/s?wd=${encodeURIComponent(v.query)}`
            }
        })
    })
})

// 豆瓣
router.get('/douban', (req, res) => {
    const options = {
        TITLE: '豆瓣',
        CACHE_KEY: 'douban',
        URL: 'https://www.douban.com/gallery'
    }
    handler(req, res, options, (res) => {
        const $ = cheerio.load(res, { decodeEntities: false })
        return $('.mod .trend').children('li').toArray().map(v => {
            return {
                title: $(v).find('a').text(),
                desc: $(v).find('a').text(),
                hotCount: $(v).find('span').text(),
                url: $(v).find('a').attr('href')
            }
        })
    })
})

// 今日头条
router.get('/toutiao', (req, res) => {
    const options = {
        TITLE: '今日头条',
        CACHE_KEY: 'toutiao',
        URL: 'https://www.toutiao.com/hot-event/hot-board/?origin=toutiao_pc'
    }
    handler(req, res, options, (res) => {
        return res.data.map(v => {
            return {
                title: v.Title,
                desc: v.QueryWord,
                hotCount: Number(v.HotValue),
                url: v.Url
            }
        })
    })
})

// 腾讯新闻
router.get('/tengxun', (req, res) => {
    const options = {
        TITLE: '腾讯新闻',
        CACHE_KEY: 'tengxun',
        URL: 'https://r.inews.qq.com/gw/event/hot_ranking_list?page_size=50'
    }
    handler(req, res, options, (res) => {
        return res.idlist[0].newslist.slice(1).map(v => {
            return {
                title: v.title,
                desc: v.nlpAbstract,
                hotCount: v.readCount,
                url: v.url
            }
        })
    })
})

// 澎湃新闻
router.get('/pengpai', (req, res) => {
    const options = {
        TITLE: '澎湃新闻',
        CACHE_KEY: 'pengpai',
        URL: 'https://cache.thepaper.cn/contentapi/wwwIndex/rightSidebar'
    }
    handler(req, res, options, (res) => {
        return res.data.hotNews.map(v => {
            return {
                title: v.name,
                desc: v.pubTime,
                pic: v.pic,
                hotCount: v.praiseTimes,
                url: `https://www.thepaper.cn/newsDetail_forward_${v.contId}`
            }
        })
    })
})

// 网易新闻
router.get('/wangyi', (req, res) => {
    const options = {
        TITLE: '网易新闻',
        CACHE_KEY: 'wangyi',
        URL: 'https://gw.m.163.com/search/api/v1/pc-wap/rolling-word'
    }
    handler(req, res, options, (res) => {
        return res.data.rollHotWordList.map(v => {
            return {
                title: v.hotWord,
                desc: v.searchWord,
                hotCount: v.source,
                url: `https://www.163.com/search?keyword=${encodeURIComponent(v.searchWord)}`
            }
        })
    })
})

// IT之家
router.get('/ithome', (req, res) => {
    const options = {
        TITLE: 'IT之家',
        CACHE_KEY: 'ithome',
        URL: 'https://m.ithome.com/rankm'
    }
    handler(req, res, options, (res) => {
        const $ = cheerio.load(res, { decodeEntities: false })
        return $('.rank-box').first().children('.placeholder').toArray().map(v => {
            return {
                title: $(v).find('.plc-title').text(),
                desc: $(v).find('.post-time').text(),
                hotCount: $(v).find('.review-num').text(),
                url: $(v).find('a').attr('href')
            }
        })
    })
})

// 哔哩哔哩
router.get('/bilibili', (req, res) => {
    const options = {
        TITLE: '哔哩哔哩',
        CACHE_KEY: 'bilibili',
        URL: 'https://api.bilibili.com/x/web-interface/ranking/v2?rid=0&type=all'
    }
    handler(req, res, options, (res) => {
        return res.data.list.map(v => {
            return {
                title: v.title,
                desc: v.owner.name,
                pic: v.pic,
                hotCount: v.stat.vv,
                url: v.short_link_v2 ? v.short_link_v2 : `https://www.bilibili.com/video/${v.bvid}`
            }
        })
    })
})

// 稀土掘金
router.get('/juejin', (req, res) => {
    const options = {
        TITLE: '稀土掘金',
        CACHE_KEY: 'juejin',
        URL: 'https://api.juejin.cn/content_api/v1/content/article_rank?category_id=1&type=hot'
    }
    handler(req, res, options, (res) => {
        return res.data.map(v => {
            return {
                title: v.content.title,
                desc: v.author.name,
                hotCount: v.content_counter.hot_rank,
                url: `https://juejin.cn/post/${v.content.content_id}`
            }
        })
    })
})

// CSDN
router.get('/csdn', (req, res) => {
    const options = {
        TITLE: 'CSDN',
        CACHE_KEY: 'csdn',
        URL: 'https://blog.csdn.net/phoenix/web/blog/hot-rank?page=0&pageSize=50'
    }
    handler(req, res, options, (res) => {
        return res.data.map(v => {
            return {
                title: v.articleTitle,
                desc: v.nickName,
                pic: v.picList[0],
                hotCount: Number(v.favorCount),
                url: v.articleDetailUrl
            }
        })
    })
})

module.exports = router